package com.qufadai.mvc.upload;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.qufadai.mvc.context.ContextHolder;
import com.qufadai.mvc.exception.UnavailableContentTypeException;
import com.qufadai.mvc.log.LogFactory;
import com.qufadai.mvc.log.Logger;

public class UploadHolder {
	static Logger log = LogFactory.getDefaultLog();
	private String fileName;
	private String fileType;
	private String targetName;

	public void setTargetName(String targetName) {
		this.targetName = targetName;
	}

	public String getTargetName() {
		return this.targetName;
	}

	public String getFileType() {
		return this.fileType;
	}

	public String getFileName() {
		return this.fileName;
	}

	public void upload() {
		HttpServletRequest request = ContextHolder.findInstance().getRequest();
		String contentType = request.getContentType();
		String boundary = request.getContentType().substring(30);
		if (contentType.indexOf("multipart/form-data") < 0)
			throw new UnavailableContentTypeException();
		try {
			ServletInputStream ins = request.getInputStream();

			byte[] buffer = new byte[1024];
			for (int i = 0; i < 4; i++) {
				int len = ins.readLine(buffer, 0, buffer.length);
				String tmp = new String(buffer, 0, len);
				if (tmp.indexOf("filename") > 0) {
					this.fileName = tmp.substring(tmp.indexOf("filename")
							+ "filename".length() + 2, tmp.lastIndexOf("\""));
					log.log("fileName : " + this.fileName);
					this.fileName = this.fileName.substring(this.fileName
							.lastIndexOf("\\"));
					this.fileType = this.fileName.substring(this.fileName
							.lastIndexOf("."));
				}
			}
			if (this.targetName == null)
				this.targetName = (ContextHolder.getCurSession()
						.getServletContext().getRealPath("/") + this.fileName);
			else {
				this.targetName += this.fileType;
			}
			OutputStream out = new BufferedOutputStream(new FileOutputStream(
					this.targetName));
			while (true) {
				int len = ins.readLine(buffer, 0, buffer.length);
				if (len == 0) {
					break;
				}
				String tmp = new String(buffer, 0, len);
				if (tmp.indexOf(boundary) > 0) {
					break;
				}
				out.write(buffer, 0, len);
			}
			int len;
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static File saveFileTo(String fileName) {
		HttpServletRequest request = ContextHolder.findInstance().getRequest();
		String boundary = request.getContentType().substring(30);
		BufferedOutputStream bos = null;
		String fileType = "";
		byte[] buffer = new byte[256];
		int len = 0;
		try {
			ServletInputStream sis = request.getInputStream();
			for (int i = 0; i < 4; i++) {
				len = sis.readLine(buffer, 0, buffer.length);
				String tmp = new String(buffer, 0, len);
				if (tmp.indexOf("filename") > 0) {
					String tmpfileName = tmp.substring(tmp.indexOf("filename")
							+ "filename".length() + 2, tmp.lastIndexOf("\""));
					log.log("fileName : " + tmpfileName);
					fileType = tmpfileName.substring(tmpfileName
							.lastIndexOf("."));
					if ((fileName.indexOf(".") < 0)
							&& (new File(fileName).isDirectory())) {
						if (tmpfileName.indexOf("/") > 0) {
							fileName = fileName
									+ tmpfileName.substring(tmpfileName
											.lastIndexOf("/"));
						}
						if (tmpfileName.indexOf("\\") > 0) {
							fileName = fileName
									+ tmpfileName.substring(tmpfileName
											.lastIndexOf("\\"));
						}
						if ((tmpfileName.indexOf("/") < 0)
								&& (tmpfileName.indexOf("\\") < 0))
							fileName = fileName + tmpfileName;
					} else {
						fileName = fileName.substring(0,
								fileName.lastIndexOf("."))
								+ fileType;
					}
				}
				if ((i > 3) && (tmp.indexOf(boundary) > 0)) {
					break;
				}
			}
			bos = new BufferedOutputStream(new FileOutputStream(fileName));
			while (true) {
				len = sis.readLine(buffer, 0, buffer.length);
				if (len == 0) {
					break;
				}
				String tmp = new String(buffer, 0, len);
				if (tmp.indexOf(boundary) > 0) {
					break;
				}
				bos.write(buffer, 0, len);
			}
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new File(fileName);
	}
}